﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace DNA.Mvc
{
    internal static class SEOHelper
    {
        private static WebSiteContext Context
        {
            get
            {
                return WebSiteContext.Current;
            }
        }
        private static HttpContextBase HttpContext
        {
            get { return Context.HttpContext; }
        }
        private static IDictionary CacheItems { get { return HttpContext.Items; } }
        private const string SEO_TITLE = "SEO_PAGE_TITLE";
        private const string SEO_DESC = "SEO_PAGE_DESC";
        private const string SEO_KEYWORDS = "SEO_PAGE_KEYWORDS";

        internal static string GetTitle()
        {
            if (CacheItems.Contains(SEO_TITLE) && CacheItems[SEO_TITLE] != null)
                return CacheItems[SEO_TITLE].ToString();

            var page = Context.Page;
            string pageTitle = "";
            if ((page != null) && (!string.IsNullOrEmpty(page.Title)))
                pageTitle = page.Title;

            var web = Context.Web;
            string websiteTitle = web != null ? Context.Web.Title : "";

            if (string.IsNullOrEmpty(websiteTitle))
                return pageTitle + " - " + websiteTitle;
            else
                return pageTitle;
        }

        internal static string GetDescription()
        {
            if (CacheItems.Contains(SEO_DESC) && CacheItems[SEO_DESC] != null)
                return CacheItems[SEO_DESC].ToString();

            var page = Context.Page;

            if ((page != null) && (!string.IsNullOrEmpty(page.Description)))
                return page.Description;

            var web = Context.Web;

            if (web != null)
                return web.Description;
            else
                return "";
        }

        internal static string GetKeywords()
        {
            if (CacheItems.Contains(SEO_KEYWORDS) && CacheItems[SEO_KEYWORDS] != null)
                return CacheItems[SEO_KEYWORDS].ToString();

            var page = Context.Page;

            if ((page != null) && (!string.IsNullOrEmpty(page.Keywords)))
                return page.Keywords;

            var web = Context.Web;
            if (web != null)
                return web.SearchKeywords;
            else
                return "";
        }

        internal static void SetTitle(string title)
        {
            CacheItems[SEO_TITLE] = title;
        }

        internal static void SetKeywords(string keywords)
        {
            CacheItems[SEO_KEYWORDS] = keywords;
        }

        internal static void SetDescription(string description)
        {
            CacheItems[SEO_DESC] = description;
        }
    }
}